package com.javaee.xiangyang.service.impl;

import com.javaee.xiangyang.common.ServiceResultEnum;
import com.javaee.xiangyang.dto.OrderGoods;
import com.javaee.xiangyang.entity.Goods;
import com.javaee.xiangyang.entity.Order;
import com.javaee.xiangyang.entity.ShopCar;
import com.javaee.xiangyang.entity.User;
import com.javaee.xiangyang.entity.enmu.impl.OrderState;
import com.javaee.xiangyang.mapper.GoodsMapper;
import com.javaee.xiangyang.mapper.OrderMapper;
import com.javaee.xiangyang.mapper.ShopCarMapper;
import com.javaee.xiangyang.mapper.UserMapper;
import com.javaee.xiangyang.service.OrderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    OrderMapper orderMapper;

    @Resource
    GoodsMapper goodsMapper;

    @Resource
    UserMapper userMapper;

    @Resource
    ShopCarMapper shopCarMapper;


    /**
     * 用户生成订单
     * 生成订单同时需要扣除用户账户里的钱
     */
    @Override
    @Transactional
    public ServiceResultEnum insertOrder(Order order) {
        if (userMapper.findByUserAccount(new User(order.getOrderUserAccount())) != null) {
            //用户存在
            if (goodsMapper.findGoodsByGoodsId(new Goods(order.getOrderGoodsId())) != null) {
                //商品也存在
                ShopCar shopCar = new ShopCar();
                shopCar.setShopUserAccount(order.getOrderUserAccount());
                shopCar.setShopGoodsId(order.getOrderGoodsId());
                ShopCar carByAccountAndId = shopCarMapper.findCarByAccountAndId(shopCar);
                if (carByAccountAndId != null) {
                    //购物车存在
                    if (userMapper.findByUserAccount(new User(order.getOrderUserAccount())).getUserMoney().subtract(carByAccountAndId.getShopTotal()).compareTo(new BigDecimal(0)) > 0) {
                        //钱够
                        //此时仓库存货减少
                        Goods goods = new Goods(carByAccountAndId.getShopGoodsId());
                        goods.setGoodsInventory(carByAccountAndId.getShopNumber());
                        goodsMapper.updateInventoryByGoodsId(goods, false);
                        //扣钱
                        User user = new User(carByAccountAndId.getShopUserAccount());
                        user.setUserMoney(carByAccountAndId.getShopTotal());
                        userMapper.updateMoneyByUserAccount(user, false);
                        //生成订单
                        order.setOrderShopTotal(carByAccountAndId.getShopTotal());
                        order.setOrderGoodsNumber(carByAccountAndId.getShopNumber());
                        orderMapper.insertOrder(order);
                        return ServiceResultEnum.SUCCESS;
                    } else {
                        //钱不够
                        return ServiceResultEnum.USER_MONEY_NOT_ENOUGH;
                    }
                }
                //购物车不存在
                return ServiceResultEnum.SHOPPING_CART_NOT_EXIST;
            }
            //商品不存在
            return ServiceResultEnum.GOODS_NOT_EXIST;
        }
        //用户不存在
        return ServiceResultEnum.LOGIN_NAME_NULL;
    }

    /**
     * 用户根据用户Id查询用户所拥有的订单
     */
    @Override
    public List<OrderGoods> findOrderByUserAccount(Order order) {
        List<OrderGoods> orderGoodsList = new ArrayList<>();
        List<Order> orderByUserAccount = orderMapper.findOrderByUserAccount(order);
        if (orderByUserAccount != null) {
            //订单不为空
            for (Order value : orderByUserAccount) {
                OrderGoods orderGoods = new OrderGoods();
                Goods goodsByGoodsId = goodsMapper.findGoodsByGoodsId(new Goods(value.getOrderGoodsId()));
                orderGoods.setGoods(goodsByGoodsId);
                orderGoods.setOrder(value);
                orderGoodsList.add(orderGoods);
            }
        }
        return orderGoodsList;
    }

    /**
     * 根据订单编号查询具体订单信息
     * 管理员用户通用
     */
    @Override
    public OrderGoods findOrderByOrderId(Order order) {
        Order orderByOrderId = orderMapper.findOrderByOrderId(order);
        if (orderByOrderId != null) {
            Goods goodsByGoodsId = goodsMapper.findGoodsByGoodsId(new Goods(orderByOrderId.getOrderGoodsId()));
            OrderGoods orderGoods = new OrderGoods();
            orderGoods.setGoods(goodsByGoodsId);
            orderGoods.setOrder(orderByOrderId);
            return orderGoods;
        }
        return null;
    }

    /**
     * 根据订单状态查找对应订单
     */
    @Override
    public List<OrderGoods> findOrderByState(Order order) {
        //找到所有对应状态下的订单
        List<Order> orderByState = orderMapper.findOrderByState(order);
        //传递的DTO封装类
        List<OrderGoods> orderGoodsList = new ArrayList<>();
        if (orderByState.size() != 0) {
            //查找内容不为空
            for (Order orders : orderByState) {
                //循环查找，挨个封装
                Goods goodsByGoodsId = goodsMapper.findGoodsByGoodsId(new Goods(orders.getOrderGoodsId()));
                OrderGoods orderGoods = new OrderGoods();
                orderGoods.setGoods(goodsByGoodsId);
                orderGoods.setOrder(orders);
                //封装结束装入容器
                orderGoodsList.add(orderGoods);
            }
            return orderGoodsList;
        }
        return null;
    }

    /**
     * 管理员查看所有订单
     * 包括商品信息
     */
    @Override
    public List<OrderGoods> findAllOrder() {
        //获取所有订单
        List<Order> allOrder = orderMapper.findAllOrder();
        //创建DTO集合
        List<OrderGoods> orderGoods = new ArrayList<>();
        for (Order order : allOrder) {
            //获取订单对应的商品信息
            Goods goodsByGoodsId = goodsMapper.findGoodsByGoodsId(new Goods(order.getOrderGoodsId()));
            //创建DTO实体
            OrderGoods orderGoods1 = new OrderGoods();
            orderGoods1.setOrder(order);
            orderGoods1.setGoods(goodsByGoodsId);
            orderGoods.add(orderGoods1);
        }
        return orderGoods;
    }

    /**
     * 管理员和用户管理订单状态
     */
    @Override
    @Transactional
    public ServiceResultEnum updateOrderStateByOrderId(Order order) {
        Order orderByOrderId = orderMapper.findOrderByOrderId(order);
        if (orderByOrderId.getOrderState() == OrderState.UNDELIVERED) {
            //未发货状态
            if (order.getOrderState() == OrderState.DELIVERED || order.getOrderState() == OrderState.CANCEL) {
                //只能改为已发货或者是取消中
                orderMapper.updateOrderStateByOrderId(order);
                return ServiceResultEnum.SUCCESS;
            }
        } else if (orderByOrderId.getOrderState() == OrderState.DELIVERED) {
            //已发货状态
            if (order.getOrderState() == OrderState.RECEIVED || order.getOrderState() == OrderState.CANCEL) {
                //只能改为取消中和已收货
                if (order.getOrderState() == OrderState.RECEIVED) {
                    //已收货更改时间状态
                    order.setOrderEnd(new Date());
                    System.out.println(new Date());
                }
                orderMapper.updateOrderStateByOrderId(order);
                return ServiceResultEnum.SUCCESS;
            }
        } else if (orderByOrderId.getOrderState() == OrderState.CANCEL) {
            //取消中状态
            if (order.getOrderState() == OrderState.FAILED_CANCELLATION || order.getOrderState() == OrderState.SUCCESSFULLY_CANCELLED || order.getOrderState() == OrderState.RECEIVED) {
                //只能改为取消失败、已取消和已收货状态
                if (order.getOrderState() == OrderState.RECEIVED || order.getOrderState() == OrderState.SUCCESSFULLY_CANCELLED) {
                    order.setOrderEnd(new Date());
                    if (order.getOrderState() == OrderState.SUCCESSFULLY_CANCELLED) {
                        //退钱
                        User user = new User(orderByOrderId.getOrderUserAccount());
                        user.setUserMoney(orderByOrderId.getOrderShopTotal());
                        userMapper.updateMoneyByUserAccount(user, true);
                    }
                }
                orderMapper.updateOrderStateByOrderId(order);
                return ServiceResultEnum.SUCCESS;
            }
        } else if (orderByOrderId.getOrderState() == OrderState.FAILED_CANCELLATION) {
            //取消失败状态
            if (order.getOrderState() == OrderState.RECEIVED) {
                //只能改为已收货
                order.setOrderEnd(new Date());
                orderMapper.updateOrderStateByOrderId(order);
                return ServiceResultEnum.SUCCESS;
            }
        }
        //错误的修改方式
        return ServiceResultEnum.ORDER_STATUS_ERROR;
    }
}
